<template>
  <div class="comment">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item, index in list" :key="index">
        <template #title>
          <div class="item">
            <van-image
              width="40"
              height="40"
              fit="cover"
              :src="item.aut_photo"
              round
            ></van-image>
            <div class="msg">
              <div class="name">{{ item.aut_name }}</div>
              <div class="com">{{ item.content }}</div>
              <div class="other">
                <span class="time">{{ item.pubdate | timefromnow }}</span>
                <van-button @click="openReply(item)" size="mini">回复（{{ item.reply_count }}）</van-button>
              </div>
            </div>
            <van-icon name="like-o" />
          </div>
        </template>
      </van-cell>
    </van-list>
    <!-- 评论输入框 -->
    <van-field
      class="myfield"
      v-model="content"
      clearable
      placeholder="请输入评论"
    >
      <template #button>
        <van-button @click="sendCom" size="small" type="info">发布</van-button>
      </template>
    </van-field>
     <!-- 回复组件 -->
    <van-action-sheet v-model="sheetShow" title="发表回复">
      <reply v-if="sheetShow" :currentCom="currentCom"></reply>
    </van-action-sheet>
  </div>
</template>

<script>
import { getCommentsApi, setCommentApi } from '@/api'
export default {
  data() {
    return {
      loading: false,
      finished: false,
      list: [],
      // 评论的内容
      content: "",
      // 获取文章 id
      art_id: this.$route.query.id,
      // 分页的标识
      offset: undefined,
      // 结束的标识
      end_id: 0,
      sheetShow: false,
      currentCom: {}
    };
  },
  methods: {
    openReply (item) {
      this.sheetShow = true
       // 将点击的数据赋值给 currentCom
      this.currentCom = item;
    },
    async sendCom() {
      const res = await setCommentApi({
        target: this.art_id,
        content: this.content,
      })
      // console.log(res);
      // 提示成功
      this.$toast.success("评论成功");
      // 更新评论：将最新的评论添加到 list 的最前面
      this.list.unshift(res.data.data.new_obj);
      // 清空输入框
      this.content = "";
    },
  async onLoad() {
    const res = await getCommentsApi({
      type: 'a',
      source: this.art_id,
      offset: this.offset,
    })
    this.list = [...this.list, ...res.data.data.results]
    // 保存下一页的标识
      this.offset = res.data.data.last_id;
      // 保存结束的标识
      this.end_id = res.data.data.end_id;
     // 将加载状态改为 false
      this.loading = false;
      // 判断数据源是否加载完毕
      if (this.offset === this.end_id) {
        this.finished = true;
      }
  },
  },
 components: {
            reply: () => import("./reply.vue")
        },
    
  
};
</script>

<style lang="less">
.comment {
  margin-bottom: 56px;
  // 在这里添加所有的样式（不会跟其它文件的样式冲突）
  margin-top: 50px;
  .item {
    display: flex;
    .msg {
      margin-left: 20px;
      flex: 1;
      .name {
        color: #4a6c9d;
      }
      .other {
        display: flex;
        .time {
          margin-right: 5px;
        }
      }
    }
  }
  .myfield {
    position: fixed;
    bottom: 0;
    left: 0;
  }
}
</style>